<?php

/**
 * 后台管理员模块 Controller
 * ============================================================================
 * TBlog TBlog博客系统　BY Tmac PHP MVC framework
 * $Author: zwt007@gmail.com $  <zwttmac@qq.com>
 * $Id: user.php 73 2011-11-22 12:50:33Z zwt007@gmail.com $
 * http://www.t-mac.org；
 */
class userAction extends Action
{

    private $tmp_model;

    /**
     * _init 方法 在执行任何Action前执行
     */
    public function _init()
    {
        $this->assign('action', $_GET['TMAC_ACTION']);
        $this->tmp_model = Tmac::model('user');
        $check_model = $this->M('check');
        $check_model->checkLogin();
        $check_model->CheckPurview('tb_admin,tb_editer');
    }

    /**
     * 管理员类别管理 首页
     */
    public function index()
    {
        //TODO  取出所有管理员
        $rs = $this->tmp_model->getUserList();
        $this->assign($rs);
        $this->V('user');
    }

    /**
     * 新增/修改管理员
     */
    public function add()
    {
        $id = !empty($_GET['uid']) ? (int) $_GET['uid'] : 0;
        $editinfo = array();

        if ($id > 0) {
            $editinfo = $this->tmp_model->getUserInfo($id);
        } else {
            $editinfo['uid'] = '';
            $editinfo['rank'] = '';
            $editinfo['username'] = '';
            $editinfo['nicename'] = '';
            $editinfo['email'] = '';
        }

        //取管理员类型option数组
        $admintype_ary = $this->tmp_model->getAdminType();
        $admin_type_option = Utility::Option($admintype_ary, $editinfo['rank'], 'rank,type_name');

        $this->assign('editinfo', $editinfo);
        $this->assign('admin_type_option', $admin_type_option);
        $this->V('user');
    }

    /**
     * 新增/修改管理员页面　保存　
     */
    public function save()
    {
        if (empty($_POST) || count($_POST) < 3) {
            $this->redirect('don\'t be evil');
            exit;
        }

        /* 初始化变量 */
        $uid = !empty($_POST['uid']) ? (int) $_POST['uid'] : 0;
        $username = !empty($_POST['username']) ? trim($this->H($_POST['username'])) : '';
        $nicename = !empty($_POST['nicename']) ? trim($this->H($_POST['nicename'])) : '';
        $password = !empty($_POST['password']) ? md5(md5($_POST['password'])) : '';
        $email = !empty($_POST['email']) ? trim($this->H($_POST['email'])) : '';
        $rank = !empty($_POST['rank']) ? $_POST['rank'] : 0;

        /* 验证不能为空 */
        if (empty($username)) {
            $this->redirect('管理员登录不能为空');
            exit;
        }
        if (empty($nicename)) {
            $this->redirect('管理员真实姓名不能为空');
            exit;
        }
        if (!isset($rank)) {
            $this->redirect('选择管理员类型!');
            exit;
        }
        // TODO goon to verify
        //验证id和admin不能重复
        $userinfo = $this->tmp_model->checkUserName($username, $uid);

        $info = array(
            'username' => $username,
            'password' => $password,
            'nicename' => $nicename,
            'email' => $email,
            'rank' => $rank,
            'reg_ip' => Functions::get_client_ip(),
            'reg_time' => time()
        );

        if ($uid > 0) {
            if (empty($password)) { //如果修改时密码为空 就不用改
                unset($info['password']);
            }
            $user_page = HttpResponse::getCookie('user_page');
            //update save article
            $where = 'uid = ' . $uid . '';
            $rs = $this->tmp_model->adduser($info, 'UPDATE', $where);
            if ($rs) {
                $this->redirect('修改管理员成功', PHP_SELF . '?m=user&page= ' . $user_page . '');
            } else {
                $this->redirect('修改管理员失败');
            }
        } else {
            //insert save article_class
            $rs = $this->tmp_model->adduser($info);
            if ($rs) {
                $this->redirect('添加管理员成功', PHP_SELF . '?m=user');
            } else {
                $this->redirect('添加管理员失败');
            }
        }
    }

    /**
     * del
     * @param int $class_id
     */
    public function del()
    {
        $do_id = (int) $_GET['uid'];
        $id_a = !empty($_POST['id_a']) ? $_POST['id_a'] : '';
        if (is_array($id_a)) {
            if (in_array(1, $id_a)) {
                $this->redirect('系统管理员不能删除!');
                exit;
            }
            $id = implode(',', $id_a);
        } elseif (!empty($do_id)) {
            $id = $do_id;
            if ($id == 1) {
                $this->redirect('系统管理员不能删除!');
                exit;
            }
        } else {
            $this->redirect('请选择要删除的...');
            exit;
        }

        $rs = $this->tmp_model->deluser($id);
        // TODO DEL该分类下的所有管理员
        if ($rs) {
            $user_page = HttpResponse::getCookie('user_page');
            $this->redirect('删除管理员成功', PHP_SELF . '?m=user&page=' . $user_page . '');
        } else {
            $this->redirect('删除管理员失败，请重试！');
        }
    }

}